package com.scpii.universal.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.scpii.universal.hibernate.dao.BaseDao;
import com.scpii.universal.hibernate.entity.FeedbackTab;
import com.scpii.universal.hibernate.intface.BaseInterface;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.SQLs;

public class FeedbackTabService implements BaseInterface<FeedbackTab> {
	private static final Logger log = LoggerFactory
			.getLogger(FeedbackTabService.class);
//	private BaseDao<FeedbackTab> feedbackTabDao = SpringBeanFactory.feedbackTabDao;
	private BaseDao<FeedbackTab> feedbackTabDao;

	public FeedbackTab get(Integer id) throws Exception {
		return feedbackTabDao.get(id);
	}

	public List<FeedbackTab> getAll() throws Exception {
		return feedbackTabDao.getAll();
	}

	public FeedbackTab get(String propertyName, Object value) throws Exception {
		return null;
	}

	public List<FeedbackTab> getList(String propertyName, Object value)
			throws Exception {
		return null;
	}

	public Long getTotalCount() throws Exception {
		return null;
	}

	public void save(FeedbackTab obj) throws Exception {
		feedbackTabDao.save(obj);
	}

	public void update(FeedbackTab obj) throws Exception {

	}

	public void delete(FeedbackTab obj) throws Exception {

	}

	public void delete(Integer id) throws Exception {

	}

	public List<FeedbackTab> hqlGetList(String hql, Map<String, Object> argsMap)
			throws Exception {
		return feedbackTabDao.hqlGetList(hql, argsMap);
	}

	public void hqlUpdate(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		feedbackTabDao.hqlUpdate(namedHqlQuery, argsMap);
	}

	public Long hqlGetNums(String namedHqlQuery, Map<String, Object> argsMap)
			throws Exception {
		return feedbackTabDao.hqlGetNums(namedHqlQuery, argsMap);
	}

	public List<FeedbackTab> sqlGetList(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return feedbackTabDao.sqlGetList(sqls, argsMap);
	}

	public void sqlUpdate(String sqls, Map<String, Object> argsMap)
			throws Exception {
		feedbackTabDao.sqlUpdate(sqls, argsMap);
	}

	public Long sqlGetNum(String sqls, Map<String, Object> argsMap)
			throws Exception {
		return feedbackTabDao.sqlGetNum(sqls, argsMap);
	}
	
	/**
	 * 根据条件获得用户留言反馈列表
	 * @param appId
	 * @param userId
	 * @param feedbackType
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-7-18上午11:16:00
	 */
	public List<FeedbackTab> getFeedbackList(int appId,int userId,int feedbackType)throws Exception{
		if (appId>0) {
			List<FeedbackTab> feedbackList = new ArrayList<FeedbackTab>();
			//设置查询参数
			Map<String,Object> argsMap = new HashMap<String, Object>();
			argsMap.put("appId", appId);
			argsMap.put("userId", userId);
			argsMap.put("feedbackType", feedbackType);
			
			//执行查询
			feedbackList = this.hqlGetList("GET_FEEDBACK_BY_USERID", argsMap);
			return feedbackList;
		}
		return null;
	}
	
	/**
	 * 获得留言反馈反馈页数
	 * @param appId
	 * @param userId
	 * @param feedbackType
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-15下午08:59:37
	 */
	public Integer getFeedBackListPageCount(int appId,Long userId,int feedbackType)throws Exception{
		if (appId>0 && userId>0) {
			//设置查询参数
			Map<String,Object> argsMap = new HashMap<String, Object>();
			argsMap.put("appId", appId);
			argsMap.put("userId", userId);
//			argsMap.put("feedbackType", feedbackType);
			
			Long resultCount = this.hqlGetNums("GET_FEEDBACK_RECORDS_NUM", argsMap);
			int result = resultCount.intValue();
			int pageCount = feedbackTabDao.getRecordPageNum(result, Const.MOBILE_CLIENT_NO_PERPAGE);
			
			return pageCount;
		}
		return 0;
	}
	
	/**
	 * 根据页码获得指定页面的反馈信息列表
	 * @param appId
	 * @param userId
	 * @param feedbackType
	 * @param pageNo
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-8-15下午09:11:14
	 */
	public List<FeedbackTab> getFeedbackListByPageNo(int appId,Long userId,int feedbackType,int pageNo)throws Exception{
		if (appId>0 && userId>0) {
			List<FeedbackTab> feedbackList = new ArrayList<FeedbackTab>();
			
			int MAXROWNUM = pageNo * Const.MOBILE_CLIENT_NO_PERPAGE;
			int MINROWNUM = (pageNo-1) * Const.MOBILE_CLIENT_NO_PERPAGE;
			
			Map<String,Object> argsMap = new HashMap<String, Object>();
			argsMap.put("appId", appId);
			argsMap.put("userId", userId);
//			argsMap.put("feedbackType", feedbackType);
			argsMap.put("MAXROWNUM", MAXROWNUM);
			argsMap.put("MINROWNUM", MINROWNUM);
			argsMap.put("intParentFeedbackId", 0);
			
			feedbackList = this.sqlGetList(SQLs.GET_FEEDBACK_LIST_BY_PAGE, argsMap);
			
			//获取留言列表
			if (feedbackList!=null && feedbackList.size()>0) {
				List<FeedbackTab> doneFeedbackList = new ArrayList<FeedbackTab>();
				for (FeedbackTab feedbackTab : feedbackList) {
					int feedbackId = feedbackTab.getIntFeedbackId();
					List<FeedbackTab> beFeedbackList = this.getFeedbackIdByParentFId(feedbackId);
					feedbackTab.setFeedbackList(beFeedbackList);
					doneFeedbackList.add(feedbackTab);
				}
				feedbackList = doneFeedbackList;
			}
			
			return feedbackList;
		}
		return null;
	}
	
	/**
	 * 获取回复的留言列表
	 * @return
	 * @throws Exception
	 * @author 甘建新
	 * 2012-9-25下午04:20:14
	 */
	public List<FeedbackTab> getFeedbackIdByParentFId(int parentFeedbackId)throws Exception{
		List<FeedbackTab> feedbackList = new ArrayList<FeedbackTab>();
		if (parentFeedbackId>0) {
			Map<String, Object> argsMap = new HashMap<String, Object>();
			argsMap.put("intParentFeedbackId", parentFeedbackId);
			
			feedbackList = this.hqlGetList("GET_FEEDBACK_BY_PARENTFEEDBACKID", argsMap);
		}
		return feedbackList;
	}

	//Setter
	public void setFeedbackTabDao(BaseDao<FeedbackTab> feedbackTabDao) {
		this.feedbackTabDao = feedbackTabDao;
	}
	
	
}